package com.example.wanbaqian.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.RefreshCallback;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2016/11/7 0007.
 * Fragment的基类
 */

public abstract class BaseFragment extends Fragment {
    private boolean isFirstVisible = true;
    private boolean isFirstInvisible = true;
    private boolean isPrepared;
    private String TAG = "BaseFragment";
    private View view;
    protected SpringView springView;
    protected Unbinder mBinder;
    protected Context mContext;
    protected int page = 1;
    protected int count = 20;
    protected boolean canLoadmore = true;
    protected Toast toast = null;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getContentViewLayoutID() != 0) {
            mContext = getActivity();
            view = inflater.inflate(getContentViewLayoutID(), container,false);
            mBinder = ButterKnife.bind(this, view);
            return view;
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }

    private Activity mActivity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    protected Toolbar mToolbar;
    protected TextView toolBarTitleTv;
    protected TextView toolBar_tvRight;
    protected ImageButton toolBar_btnRight;

    protected void setToolBar(final String title) {
        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolBarTitleTv = (TextView) view.findViewById(R.id.toolBar_title);
        toolBar_tvRight = (TextView) view.findViewById(R.id.toolBar_tvRight);
        toolBar_btnRight = (ImageButton) view.findViewById(R.id.toolBar_btnRight);
        toolBarTitleTv.setText(title);
        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.back_white);
        // 不显示标题和子标题
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    //得到可靠地Activity
    public Activity getMyActivity() {
        return mActivity;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViewsAndEvents(view);
    }

    protected abstract int getContentViewLayoutID();

    protected abstract void initViewsAndEvents(View view);

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG, "onActivityCreated: onActivityCreated");
        initPrepare();
    }

    private synchronized void initPrepare() {
        Log.d(TAG, "initPrepare: " + isPrepared);
        if (isPrepared) {
            onFirstUserVisible();
        } else {
            isPrepared = true;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d(TAG, "setUserVisibleHint: " + isVisibleToUser);
        if (isVisibleToUser) {
            if (isFirstVisible) {
                isFirstVisible = false;
                initPrepare();
            } else {
                onUserVisible();
            }
        } else {
            if (isFirstInvisible) {
                isFirstInvisible = false;
                onFirstUserInvisible();
            } else {
                onUserInvisible();
            }
        }
    }

    protected SpringView setHeaderAndFooter(int viewId, boolean isShowHeader, boolean isShowFooter, final RefreshCallback callback) {
        springView = (SpringView) view.findViewById(viewId);
        springView.setType(SpringView.Type.FOLLOW);
        if (isShowHeader) {
            springView.setHeader(new DefaultHeader(mContext));
        }
        if (isShowFooter) {
            springView.setFooter(new DefaultFooter(mContext));
        }
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                callback.getNetDate();
            }

            @Override
            public void onLoadmore() {
                Log.i(TAG, " page:" + page + "------count:" + count + "------canLodMore:" + canLoadmore);
                if (canLoadmore) {
                    page++;
                    callback.getNetDate();
                } else {
                    springView.onFinishFreshAndLoad();
                    showToast(getString(R.string.no_loding_more));
                }
            }
        });
        return springView;
    }

    protected abstract void onFirstUserVisible();

    protected abstract void onUserVisible();

    private void onFirstUserInvisible() {
    }

    protected abstract void onUserInvisible();

    @Override
    public void onDestroy() {
        mBinder.unbind();
        DetoryViewAndThing();
        super.onDestroy();
    }

    protected abstract void DetoryViewAndThing();


    protected void openActivity(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    protected void openActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    protected void openActivityForResult(Class<?> clazz, int requestCode) {
        Intent intent = new Intent(getActivity(), clazz);
        getActivity().startActivityForResult(intent, requestCode);
    }

    protected void openActivityForResult(Class<?> clazz, int requestCode, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        getActivity().startActivityForResult(intent, requestCode);
    }

    protected void showToast(String message) {
        if (!TextUtils.isEmpty(message)) {
            if (toast == null) {
                toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
            } else {
                toast.setText(message);
            }
            toast.show();
        }
    }
}
